package com.stack.wemedia.job;
import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.stack.apis.admin.IAdminClient;
import com.stack.apis.article.IArticleClient;
import com.stack.common.utils.thread.WmThreadLocalUtil;
import com.stack.file.service.FileStorageService;
import com.stack.model.admin.dtos.AdNewsDto;
import com.stack.model.article.dtos.ArticleDto;
import com.stack.model.common.dtos.ResponseResult;
import com.stack.model.wemedia.dtos.WmNewsDto;
import com.stack.model.wemedia.pojos.*;
import com.stack.wemedia.mapper.WmChannelMapper;
import com.stack.wemedia.mapper.WmMaterialMapper;
import com.stack.wemedia.mapper.WmUserMapper;
import com.stack.wemedia.service.NewsCrawlerService;
import com.stack.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;

@Slf4j
@Component
public class YiCar {

    @Resource
    private NewsCrawlerService newsCrawlerService;
    @Resource
    private WmNewsService wmNewsService;
    @Resource
    private WmMaterialMapper wmMaterialMapper;
    @Resource
    private FileStorageService fileStorageService;
    @Resource
    private IAdminClient adminClient;
    @Resource
    private WmUserMapper wmUserMapper;
    @Resource
    private WmChannelMapper wmChannelMapper;
    //    @Qualifier("com.stack.apis.article.IArticleClient")
//    @Autowired
    @Resource
    private IArticleClient articleClient;

    public void collectNews() throws IOException {
        getUrl();
        getDetails();
    }

    private void getDetails() {
        // 模拟当前自媒体登录用户
        WmUser wmUser = new WmUser();
        wmUser.setId(1103);
        WmThreadLocalUtil.setUser(wmUser);

        List<NewsCrawler> newsList = newsCrawlerService.getNews(7);
        log.info("本次爬取的url数量: {}", newsList.size());

        for (NewsCrawler news : newsList) {
            String url = news.getUrl();
            String title = news.getTitle();
            log.info("本次爬取url: {}", url);

            // 获取该网页document文档数据
            Document document = null;
            Element content = null;

            try {
                document = Jsoup.connect(url)
                        .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36")
                        .get();
                content = document.getElementsByClass("news-detail-main motu_cont").get(0);
                log.info("document: {}", document);
            } catch (Exception e) {
                log.error("获取新闻详情失败,原因是{}", e.getMessage());
                continue;
            }

            log.info("获取新闻详情成功: {}", content);
            Elements elements = content.getElementsByTag("p");
            List<Map> contentMap = new ArrayList<>();
            List<String> imgUrlList = new ArrayList<>();
            for (Element element : elements) {
                Map<String, String> map = null;

                String text = element.text();
                if (StringUtils.isNotBlank(text)) {
                    log.info("新闻内容: {}", text);
                    map = new HashMap<>();
                    map.put("type", "text");
                    map.put("value", text);
                    contentMap.add(map);
                }

                String imgUrl = element.getElementsByTag("img").attr("data-original");
                if (StringUtils.isNotEmpty(imgUrl) && imgUrl.contains("https://image.bitautoimg.com")) {
                    log.info("新闻图片: {}", imgUrl);
                    String fileName = uploadPic(imgUrl);
                    imgUrlList.add(fileName);
                    map = new HashMap<>();
                    map.put("type", "image");
                    map.put("value", fileName);
                    contentMap.add(map);
                }

            }

            log.info("解析后的图片: {}", imgUrlList);
            log.info("解析后的内容: {}", contentMap);

            // 上传图片//保存素材
            for (String imgUrl : imgUrlList) {
                WmMaterial wmMaterial = null;
                if (StringUtils.isNotBlank(imgUrl)) {
                    // 如果上传图片路径不为空  创建素材信息
                    wmMaterial = new WmMaterial();
                    wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
                    wmMaterial.setUrl(imgUrl);
                    wmMaterial.setType((short) 0);
                    wmMaterial.setIsCollection((short) 0);
                    wmMaterial.setCreatedTime(new Date());
                    log.info("插入素材信息: {}", wmMaterial);
                    wmMaterialMapper.insert(wmMaterial);
                }
            }

            try {
                // 保存自媒体文章
                WmNewsDto wmNewsDto = new WmNewsDto();
                wmNewsDto.setContent(JSON.toJSONString(contentMap));
                wmNewsDto.setTitle(title);
                log.info("文章封面: {}", imgUrlList);
                if (imgUrlList.size() > 0) {
                    wmNewsDto.setType((short) 1);
                    List<String> imgUrls = Lists.newArrayList(imgUrlList.get(0));
                    wmNewsDto.setImages(imgUrls);
                } else {
                    wmNewsDto.setType((short) 0);
                }

                wmNewsDto.setChannelId(7);
                try {
                    Thread.sleep(1000); // 睡眠1秒 让发布时间不一致
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                wmNewsDto.setPublishTime(new Date());
                wmNewsDto.setStatus((short) 1); //
                wmNewsDto.setLabels("采集");
                log.info("保存自媒体文章: {}", wmNewsDto);
                wmNewsService.submitNews(wmNewsDto);

                // 保存admin端的相关的审核文章数据
                AdNewsDto dto = new AdNewsDto();
                dto.setUserId(WmThreadLocalUtil.getUser().getId());
                dto.setAuthorName("crawler菜鸟");
                dto.setTitle(title);
                dto.setContent(JSON.toJSONString(contentMap));
                dto.setStatus(9);
                dto.setChannelId(7);
                dto.setLabels("采集");
                dto.setCreatedTime(new Date());
                dto.setSubmitedTime(new Date());
                if (imgUrlList.size() > 0) {
                    dto.setType(1);
                    List<String> imgUrls = Lists.newArrayList(imgUrlList.get(0));
                    dto.setImages(imgUrls);
                } else {
                    dto.setType(0);
                }
                dto.setEnable(0);
                log.info("保存admin端的相关的审核文章数据: {}", dto);
                adminClient.saveAdCheckNews(dto);
            } catch (Exception e) {
                log.error("保存自媒体文章失败:{}", e.getMessage());
                log.error("保存admin端的相关的审核文章数据失败");
            } finally {
                //修改爬虫配置表
                NewsCrawler newsCrawler = new NewsCrawler();
                newsCrawler.setUrl(url);
                newsCrawler.setStatus(1);
                newsCrawlerService.update(newsCrawler);
            }
        }
    }

    private void getUrl() throws IOException {
        String url = "https://news.yiche.com/info/categoryId0_p80-9999_l9_f0_g0_c0_b0_5.html"; //文章筛选

        // 获取该网页document文档数据
        Document document = Jsoup.connect(url)
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36")
                .get();

        log.info("document: {}", document);
        Elements elements = document.getElementsByClass("article-card");
        log.info("文章url数量: {}", elements.size());
        for (Element element : elements) {
            Element element1 = element.getElementsByTag("h2").get(0).getElementsByTag("a").get(0);
            String newsUrl = "https://news.yiche.com" + element1.attr("href");
            String title = element1.text();
            log.info("新闻的url: {}", newsUrl);
            log.info("标题: {}", title);

            try {
                NewsCrawler newsCrawler = new NewsCrawler();
                newsCrawler.setTitle(title);
                newsCrawler.setUrl(newsUrl);
                newsCrawler.setChannel(7);
                newsCrawler.setStatus(0);
                newsCrawler.setCreatedTime(new Date());
                newsCrawlerService.insert(newsCrawler);
                log.info("爬爬爬爬爬爬爬!");
            } catch (Exception e) {
                log.error("标题不合规，爬取失败: {}", e.getMessage());
            }
        }
    }


    /**
     * 上传图片
     *
     * @param imgUrl
     * @return
     */
    public String uploadPic(String imgUrl) {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String suffix = imgUrl.substring(imgUrl.lastIndexOf("."));
        InputStream in = getInputStreamByUrl(imgUrl);
        if (in != null) {
            String fileName = fileStorageService.uploadImgFile("", uuid + suffix, in);
            System.out.println("上传文件名称: " + fileName);
            return fileName;
        }
        return null;
    }

    /**
     * 工具方法:  根据url路径 获取输入流
     *
     * @param strUrl
     * @return
     */
    public static InputStream getInputStreamByUrl(String strUrl) {
        HttpURLConnection conn = null;
        try {
            URL url = new URL(strUrl);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(20 * 1000);
            final ByteArrayOutputStream output = new ByteArrayOutputStream();
            IOUtils.copy(conn.getInputStream(), output);
            return new ByteArrayInputStream(output.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (conn != null) {
                    conn.disconnect();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 保存app端相关的文章数据
     *
     * @param wmNews
     */
    private ResponseResult saveAppArticle(WmNews wmNews) {

        ArticleDto dto = new ArticleDto();
        //属性的拷贝
        BeanUtils.copyProperties(wmNews, dto);
        //文章的布局
        dto.setLayout(wmNews.getType());
        //频道
        WmChannel wmChannel = wmChannelMapper.selectById(wmNews.getChannelId());
        if (wmChannel != null) {
            dto.setChannelName(wmChannel.getName());
        }

        //作者
        dto.setAuthorId(wmNews.getUserId().longValue());
        WmUser wmUser = wmUserMapper.selectById(wmNews.getUserId());
        if (wmUser != null) {
            dto.setAuthorName(wmUser.getName());
        }

        //设置文章id
        if (wmNews.getArticleId() != null) {
            dto.setId(wmNews.getArticleId());
        }
        dto.setCreatedTime(new Date());

        log.info("保存的文章:{}", JSON.toJSONString(dto));
        ResponseResult responseResult = articleClient.saveArticle(dto);

        return responseResult;
    }

}
